package Backtracking;

import java.util.ArrayList;
import java.util.List;

public class 组合 {

    List<List<Integer>> res = new ArrayList<>();
    int max = 0;
    int len = 0;

    public List<List<Integer>> combine(int n, int k) {
        max = n;
        len = k;
        backTracking(new ArrayList<>(), 1);
        return res;
    }

    private void backTracking(List<Integer> comb, int num) {
        if (comb.size() == len) {
            res.add(new ArrayList<>(comb));
            return;
        }
        for (int i = num; i <= max - (len - comb.size()) + 1; i++) {
            comb.add(i);
            backTracking(comb, i + 1);
            comb.removeLast();
        }
    }
}
